package com.cjie.ecommerce.dao;

import com.cjie.ecommerce.entity.Product;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Repository
public class ProductRepository {
    private static final Logger log = LoggerFactory.getLogger(ProductRepository.class);

    /**
     * 模拟从数据库获取商品列表
     *
     * @return
     */
    public List<Product> mockProductsFromDB() {
        log.info("模拟从数据库查询商品列表");
        List<Product> products = new ArrayList<>();

        // 模拟数据
        products.add(new Product(20001L, "iPhone 13", "a0001", new BigDecimal("5999.00"), "苹果最新款手机", 100, "http://example.com/iphone13.jpg"));
        products.add(new Product(20002L, "小米12", "a0001", new BigDecimal("3699.00"), "小米旗舰手机", 200, "http://example.com/mi12.jpg"));
        products.add(new Product(20003L, "华为MateBook", "a0002", new BigDecimal("6999.00"), "华为笔记本电脑", 50, "http://example.com/matebook.jpg"));

        // 模拟数据库查询延迟
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        return products;
    }

    /**
     * 模拟从数据库获取单个商品
     */
    public Product mockProductFromDB(Long id) {
        log.info("模拟从数据库查询商品ID={}", id);

        // 模拟数据
        if (id == 10001L) {
            return new Product(10001L, "iPhone 13", "a0001", new BigDecimal("5999.00"),
                    "苹果最新款手机，A15仿生芯片，超视网膜XDR显示屏", 100, "http://example.com/iphone13.jpg");
        } else if (id == 10002L) {
            return new Product(10002L, "小米12", "a0001", new BigDecimal("3699.00"),
                    "小米旗舰手机，骁龙8 Gen1处理器，120Hz高刷屏", 200, "http://example.com/mi12.jpg");
        } else if (id == 10003L) {
            return new Product(10003L, "华为MateBook", "a0002", new BigDecimal("6999.00"),
                    "华为笔记本电脑，14英寸2K触控全面屏，第11代英特尔酷睿处理器", 50, "http://example.com/matebook.jpg");
        }

        // 模拟数据库查询延迟
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        return null;
    }
}
